﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Gagnatek.WebSite.Models;

namespace Gagnatek.WebSite.Controllers
{ 
    public class MediasController : Controller
    {
        private GagnatekDBEntities db = new GagnatekDBEntities();

        //
        // GET: /Medias/

        // [Authorize(Roles = "Owner")]
        public ViewResult Index()
        {
            var medias = db.Medias.Include(m => m.Authors).Include(m => m.MediaTypes);
            return View(medias.ToList());
        }

        public ViewResult Search()
        {
            var query = Request["search"];

            var q = from m in db.Medias
                    where m.name.Contains(query) ||
                     m.description.Contains(query) ||
                     m.Authors.name.Contains(query)
                    select m;

            return View("Index", q.ToList());
        }

        //
        // GET: /Medias/Details/5

        public ViewResult Details(int id)
        {
            Medias medias = db.Medias.Find(id);
            return View(medias);
        }

        //
        // GET: /Medias/Create

        public ActionResult Create()
        {
            ViewBag.author_id = new SelectList(db.Authors, "id", "name");
            ViewBag.type_id = new SelectList(db.MediaTypes, "id", "name");
            return View();
        } 

        //
        // POST: /Medias/Create

        [HttpPost]
        public ActionResult Create(Medias medias)
        {
            if (ModelState.IsValid)
            {
                db.Medias.Add(medias);
                db.SaveChanges();
                return RedirectToAction("Index");  
            }

            ViewBag.author_id = new SelectList(db.Authors, "id", "name", medias.author_id);
            ViewBag.type_id = new SelectList(db.MediaTypes, "id", "name", medias.type_id);
            return View(medias);
        }
        
        //
        // GET: /Medias/Edit/5
 
        public ActionResult Edit(int id)
        {
            Medias medias = db.Medias.Find(id);
            ViewBag.author_id = new SelectList(db.Authors, "id", "name", medias.author_id);
            ViewBag.type_id = new SelectList(db.MediaTypes, "id", "name", medias.type_id);
            return View(medias);
        }

        //
        // POST: /Medias/Edit/5

        [HttpPost]
        public ActionResult Edit(Medias medias)
        {
            if (ModelState.IsValid)
            {
                db.Entry(medias).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            ViewBag.author_id = new SelectList(db.Authors, "id", "name", medias.author_id);
            ViewBag.type_id = new SelectList(db.MediaTypes, "id", "name", medias.type_id);
            return View(medias);
        }

        //
        // GET: /Medias/Delete/5
 
        public ActionResult Delete(int id)
        {
            Medias medias = db.Medias.Find(id);
            return View(medias);
        }

        //
        // POST: /Medias/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {            
            Medias medias = db.Medias.Find(id);
            db.Medias.Remove(medias);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}